﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PipeLineManager : MonoBehaviour
{
    public GameObject template;
    List<PipeLine> pipeLines = new List<PipeLine>();
    public float Speed;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    public void Init()
    {
        for (int i = 0; i < pipeLines.Count; i++)
        {
            Destroy(pipeLines[i].gameObject);
        }
        pipeLines.Clear();
    }
    Coroutine coroutine = null;
    public void StartRun()
    {
      
            coroutine= StartCoroutine(GreatPipeLines());
       

    }
    public void Stop()
    {
        StopCoroutine(coroutine);

        for (int i = 0; i < pipeLines.Count; i++)
        {
            pipeLines[i].enabled = false;
        }
    }
    IEnumerator GreatPipeLines()
    {

        
        
        for (int i = 0; i < 3; i++)
        {

            if (pipeLines.Count<3)
            {
                GreatPipeLine();
            }
            else
            {
                pipeLines[i].enabled = true;
                pipeLines[i].Init();

            }
            
            yield return new WaitForSeconds(Speed);
        }
    }

    public void GreatPipeLine()
    {
        if (pipeLines.Count<3)
        {
            GameObject obj = Instantiate(template, this.transform);
            PipeLine pipeLine = obj.GetComponent<PipeLine>();
            pipeLines.Add(pipeLine);
        }
   
    }
}
